package jinyilw.swing.visuals.panel;

import jinyilw.swing.ColorTools;
import jinyilw.swing.FontTools;
import jinyilw.swing.i18n.ICSetting;
import jinyilw.swing.i18n.IString;
import jinyilw.swing.rlaf.utils.RBorderFactory;
import jinyilw.swing.visuals.Icomp.ILabel;
import jinyilw.swing.visuals.Jcomp.TransButton;
import jinyilw.swing.visuals.Rcomp.RButton;
import jinyilw.swing.visuals.comp.ColorComp;

import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class PalettePanel extends JPanel // 调色板
{
	private static final long serialVersionUID = 6562275386151337808L;

	public Color currentColor = null;
	private boolean enable = true;

	private final JPanel northPanel = new JPanel();
	private final ILabel staticLabel = new ILabel();
	private final ColorComp currentPanel = new ColorComp();
	private final JPanel centerPanel = new JPanel();
	private final JPanel southPanel = new JPanel();
	private final RButton setOrtherButton = new RButton();
	private final TransButton setNull = new TransButton();
	private final JPanel nullColor = new JPanel();

	private ColorComp[] colorButtons;
	private final Color[] colors = {Color.BLACK, Color.BLUE, Color.CYAN,
			Color.GREEN, Color.MAGENTA, Color.RED, Color.YELLOW, Color.PINK,
			Color.ORANGE, Color.WHITE, new Color(102, 0, 51),
			new Color(153, 153, 255), Color.GRAY, new Color(138, 43, 226),
			new Color(0, 153, 153)};
	private final ColorButtonsListener colorButtonsListener = new ColorButtonsListener();

	public PalettePanel()
	{
		super();
		initGUI();
	}

	private void initGUI()
	{
		// setBorder(new TitledBorder(RBorderFactory.createRoundBorder(2, 0),
		// "调色板", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION,
		// UIData.fonts[13]));
		// Icon icon = ResourceManager.getResLazyRetinalIcon("color.png");
		// setBorder(RBorderFactory.createTitleBorder(icon, "调色板"));
		// setFont(UIData.defaultFont);
		// setOpaque(false);
		setLayout(new BorderLayout(2, 1));
		add(northPanel, BorderLayout.NORTH);
		northPanel.setOpaque(false);
		northPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 2, 0));
		northPanel.add(staticLabel);
		staticLabel
				.setText(new ICSetting("currentColor", "当前颜色: ", "Current:"));
		// staticLabel.setFont(UIData.defaultFont);
		northPanel.add(nullColor);
		nullColor.setBorder(RBorderFactory.createRoundBorder(0, 18, 0));
		nullColor.setPreferredSize(new Dimension(22, 22));
		currentPanel.setPreferredSize(new Dimension(22, 22));

		add(centerPanel, BorderLayout.CENTER);
		centerPanel.setOpaque(false);
		centerPanel.setLayout(new GridLayout(0, 5, 1, 1));
		centerPanel.setBorder(new EmptyBorder(3, 4, 3, 4));
		colorButtons = new ColorComp[colors.length];
		for (int i = 0; i < colors.length; i++)
		{
			colorButtons[i] = new ColorComp();
			colorButtons[i].setBackground(colors[i]);
			colorButtons[i].addMouseListener(colorButtonsListener);
			centerPanel.add(colorButtons[i]);
		}

		add(southPanel, BorderLayout.SOUTH);
		southPanel.setOpaque(false);
		southPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 2, 0));
		setNull.addActionListener(new SetNullActionListener());
		setNull.setFont(FontTools.getCustomFont(11));
		setNull.setText(new ICSetting("nulltColor", "无颜色", "null"));
		setNull.setPreferredSize(new Dimension(42, 20));
		setNull.setBorder(RBorderFactory.getNoShadowBorder());
		setOrtherButton.addActionListener(new SetOrtherButtonActionListener());
		setOrtherButton.setText("...");
		// setOrtherButton.setToolTipText("自定义颜色");
		setOrtherButton.setPreferredSize(new Dimension(25, 18));
		setOrtherButton.configAlpha(0.5f, 0.6f);
		southPanel.add(setNull);
		southPanel.add(setOrtherButton);
	}

	public void setColumn(int col)
	{
		centerPanel.setLayout(new GridLayout(0, col, 1, 1));
	}

	public void setEnable(boolean b)
	{
		enable = b;
		setOrtherButton.setEnabled(b);
		setNull.setEnabled(b);
		if (!enable && currentColor != null)
		{
			currentColor = null;
			setOrtherButton.setBackground(null);
			northPanel.remove(currentPanel);
			northPanel.add(nullColor);
			northPanel.validate();
			nullColor.repaint();
		}
	}

	private class ColorButtonsListener extends MouseAdapter
	{
		@Override
		public void mouseClicked(final MouseEvent e)
		{
			if (enable)
			{
				Color color = ((ColorComp) e.getSource()).getBackground();
				currentPanel.setBackground(color);
				if (currentColor == null)
				{
					northPanel.remove(nullColor);
					northPanel.add(currentPanel);
					northPanel.validate();
					currentPanel.repaint();
				}
				currentColor = color;
				setOrtherButton.setBackground(null);
			}
		}
	}

	private class SetNullActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(final ActionEvent e)
		{
			if (currentColor != null)
			{
				currentColor = null;
				setOrtherButton.setBackground(null);
				northPanel.remove(currentPanel);
				northPanel.add(nullColor);
				northPanel.validate();
				nullColor.repaint();
			}
		}
	}

	private class SetOrtherButtonActionListener implements ActionListener
	{
		IString setCustomString = new IString("setCustom", "自定义颜色",
				"Set custom color");

		@Override
		public void actionPerformed(final ActionEvent e)
		{
			Color color = ColorTools.showDefaultColorDialog(null,
					setCustomString, currentColor);
			if (color != null)
			{
				currentPanel.setBackground(color);
				if (currentColor == null)
				{
					northPanel.remove(nullColor);
					northPanel.add(currentPanel);
					northPanel.validate();
					currentPanel.repaint();
				}
				currentColor = color;
				setOrtherButton.setBackground(color);
			}
		}
	}
}
